
$(function () {
	basics.siderMenu();
	$("#logout").bind("click", function () {
		basics.logout();
	});
	
	basics.initIframeUrl()
	basics.siderSubMenu();
	basics.getUserInfo();
	$('.ui-tips,.mini-tips').tipsy({gravity: 's'});

});

//侧边菜单
basics.siderMenu = function () {
	if (store.session.has('basicsMenu')) {
		var arr = store.session.get('basicsMenu');
		basics.setSideMenuHtml(arr, "#silderMenuBox");
		basics.initSideMenu();
	} else {
		$.ajax({
			url: "/portal/index/getServiceList",
			type: "get",
			success: function (res) {
				if (res.code == 0) {
					var arr = res.data;
					store.session.set('basicsMenu', arr);
					basics.setSideMenuHtml(arr, "#silderMenuBox");
					basics.initSideMenu();
				} else {
					 x.miniAlert('fail',res.msg,1500)
				}
			}
		});
	}

};

//设置侧边菜单
basics.setSideMenuHtml = function (arr, id) {
	var html = "";
	html += '<li data-key="home">';
	html += '    <a href="/portal/index" class="icon-sideMenu" >';
	html += '        <span class="icon-home-sider icon-sider"></span>';
	html += '		  <i>首页</i>';
	html += '    </a>';
	html += '</li>';
	if(arr.length>0){
		
		$.each(arr, function (i, v) {
			html += '<li class="" data-key="service-' + v.id + '">';
			html += '    <a href="/portal/service?serviceId=' + v.id + '&menuId='+v.linkMenuId+'" class="icon-sideMenu"  >';
			html += '        <span class="' + v.icon + '-sider icon-sider"></span>';
			html += '        <i>' + v.name + '</i>';
			html += '    </a>';
			html += '</li>';
		});
	}
	$(id).empty().html(html);
	basics.setSideMenuScroll();
	basics.loadSideMenuCur(id)
	

};

basics.setSideMenuScroll = function(){
	$('.icon-sideMenu').click(function (e) {
		var i = $(this).parent().index();
		var t = $('.silder').scrollTop();
			var silderTops = t + $('.icon-sideMenu').eq(i).offset().top - $('.silder').height()/2 + $('.icon-sideMenu').height()*0.5;
		$('.silder').stop().animate({
 			scrollTop: silderTops
		}, 0);
		store.session.set('silderTop', silderTops);
	})

	if (store.session.has('silderTop')) {
		$('.silder').stop().animate({
 			scrollTop: store.session.get("silderTop")
		}, 0);
	}
};

//加载初始化
basics.loadSideMenuCur = function (id) {
	var serviceId = common.getUrlKey("serviceId");
	
	if (serviceId) {
		$(id+" li[data-key='service-"+serviceId+"']").addClass("cur");
		return;
	}
	$(id+" li[data-key='home']").addClass("cur");

};

//处理侧边菜单
basics.initSideMenu = function () {
	$("#side").mouseover(function(){
		$("body").addClass("silderMenu-hover");
		$("#side").addClass("silderMenu-hover")
	});
	$("#side").mouseleave(function(){
		$("body").removeClass("silderMenu-hover")
		$("#side").removeClass("silderMenu-hover")
	});
	
	$(".icon-sideMenu").mouseover(function(){
		$("body").addClass("silderMenu-hover");
		$("#side").addClass("silderMenu-hover")
	});
	$(".icon-sideMenu").mouseleave(function(){
		$("body").removeClass("silderMenu-hover")
		$("#side").removeClass("silderMenu-hover")
	});
};

//获取侧边菜单2级菜单
basics.siderSubMenu = function () {
	if($("#sideSub").size()>0){
	var serviceId =common.getUrlKey("serviceId");
		$.ajax({
			url: "/portal/service/getMenuList?serviceId="+serviceId,
			type: "get",
			success: function (res) {
				if (res.code == 0) {
					var arr = res.data;
					basics.setSiderSubMenuHtml(arr, "#sideSubBox");
				} else {
					x.miniAlert('fail',res.msg,1500)
				}
			}
		});
	}
};


//设置侧边菜单
basics.setSiderSubMenuHtml = function (arr, id) {
	
	var menu = arr.menus;
	var service = arr.service;
	var guildHtml = "";
	var html = "";
	
	html += '<div class="sideSub-menu">';
	html += '    <div class="sideSub-title">';
	html += '    	<i class="'+service.icon+'-home icon-sideSub"></i>';
	html += '       <span>'+service.name+'</span>';
	html += '   </div>';
	html += '   <div class="sideSub-list">';
	
	$.each(menu, function (i, v) {
		if(menu.length>1){
			html += '   	<div class="sideSubMenu-title">'+v.name+'</div>';
		}
		html += '       <ul class="sideSubMenu">';
		$.each(v.meuns, function(s, n) {
			html += '        <li><a data-menuId="'+n.id+'" href="/portal/service?serviceId='+common.getUrlKey("serviceId")+'&menuId='+n.id+'">'+n.name+'</a></li>';
		})
		html += '      </ul>';
	});
	html += '  </div>';
	html += '</div>';
	$(id).empty().html(html);
	
	var menuId = common.getUrlKey('menuId');
	var code = common.getUrlKey("code");
	var param = common.getUrlKey("param");
	if (!menuId) {
		menuId = $("#sideSubBox li a").eq(0).attr("data-menuId");
	}
	$("#sideSubBox li").find("a[data-menuId='"+menuId+"']").parent().addClass("cur");
	var curSrc = "/portal/menu/link?menuId="+menuId+"&code="+code+"&param="+param;

	basics.iframeUrl("#iframe",curSrc);
	
};

//加载iframe
basics.iframeUrl = function(id,url){
	if(url != undefined){
		var t = '_t='+Math.floor(Math.random()*1000000);
		if(url.indexOf('?')==-1){
			url+="?"+t;
		}else{
			url+="&"+t;
		}
		$(id).attr("src",url);
	}
	
	$(id).load(function(){
	　　setTimeout(function(){
			$("#homeLoading").hide();
		},1000)
	});
};

//获取头部信息
basics.getUserInfo=function(){
	if (store.session.has('basicsUserInfo')) {
		var item = store.session.get('basicsUserInfo');
		basics.getUserInfoHtml(item);
	} else {
		$.ajax({
			url: "/portal/index/getUserInfo",
			type: "get",
			success: function (res) {
				if (res.code == 0) {
					var item = res.data;
					store.session.set('basicsUserInfo', item)
					basics.getUserInfoHtml(item);
				} else {
					x.miniAlert('fail',res.msg,1500)
				}
			}
		});
	}
};
//设置头部
basics.getUserInfoHtml=function(item){
//	$("#orgLogo").attr("src",item.hmoLogo);
//	$("#orgName,#orgNameMini").html(item.hmoName);
	$("#loginName").html(item.loginName);
};



//退出
basics.logout = function () {
	store.session.remove('basicsMenu');
	store.session.remove('basicsUserInfo');
	store.session.remove('silderTop');
//	$.removeCookie('SideMenuCur', { path: '/' });
	$.removeCookie('upgradeHmoCheck', { path: '/' });
	window.location.href = "/portal/logout";
};


//机构信息//修改密码
basics.initIframeUrl = function () {
	var menuId = common.getUrlKey("menuId");
	var code = common.getUrlKey("code");
	var param = common.getUrlKey("param");
	if (!code && $("#iframe").length > 0 && $("#iframe").attr("data-code")) {
		code = $("#iframe").attr("data-code");
	}
	if(code && !menuId){
		var url = "/portal/iframe/link?code="+code+"&param"+param;
		basics.iframeUrl("#iframe",url);
	}
};

